//
//  Login.m
//  Whopaidforit
//
//  Created by Le Dung on 2/21/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "Login.h"

@interface Login ()<UITextFieldDelegate>


@end

@implementation Login
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)createContent
{
    self.contenView =[[UIView alloc]initWithFrame:CGRectMake(20, 190, 280, 260)];
        
    self.name =[[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 30, 30)];
    self.name.image=[UIImage imageNamed:@"loginAcc"];
    
    self.mail =[[UIImageView alloc]initWithFrame:CGRectMake(20, 60, 30, 30)];
    [self.mail setBackgroundColor:[UIColor brownColor]];
    self.mail.image=[UIImage imageNamed:@"loginMail"];

    self.pass =[[UIImageView alloc]initWithFrame:CGRectMake(20, 110, 30, 30)];
    [self.pass setBackgroundColor:[UIColor brownColor]];
    self.pass.image=[UIImage imageNamed:@"loginPass"];
    
    self.nameField =[[UITextField alloc]initWithFrame:CGRectMake(60, 15, 200, 40)];
    [self.nameField setPlaceholder:@"Name"];
    self.nameField.delegate=self;
    
    self.mailField =[[UITextField alloc]initWithFrame:CGRectMake(60, 65, 200, 40)];
    [self.mailField setPlaceholder:@"Email"];
    self.mailField.delegate=self;


    self.passField =[[UITextField alloc]initWithFrame:CGRectMake(60, 115, 200, 40)];
    [self.passField setPlaceholder:@"Password"];
    self.passField.delegate=self;
    [self.passField setSecureTextEntry:YES];

    
    self.signUp =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.signUp setFrame:CGRectMake(20, 170, 240, 40)];
    [self.signUp setTitle:@"Sign Up" forState:UIControlStateNormal];
    self.signUp.titleLabel.textAlignment= UITextAlignmentCenter;
    [self.signUp setBackgroundImage:[UIImage imageNamed:@"btLogin"] forState:UIControlStateNormal];
    [self.signUp addTarget:self action:@selector(signAction) forControlEvents:UIControlEventTouchUpInside];

    
      //----------------------------------------------OR
    self.orView =[[UIView alloc]initWithFrame:CGRectMake(20, 155, 280, 30)];
    
    UIImageView *line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 15, 100, 1)];
    [line setBackgroundColor:[UIColor brownColor]];
    [self.orView addSubview:line];
    [line release];
    
    UIImageView *line1 =[[UIImageView alloc]initWithFrame:CGRectMake(177, 15, 100, 1)];
    [line1 setBackgroundColor:[UIColor brownColor]];
    [self.orView addSubview:line1];
    [line1 release];
    
    UILabel *orL =[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 80, 30)];
    orL.text=@"OR";
    orL.textAlignment=NSTextAlignmentCenter;
    [orL setBackgroundColor:[UIColor clearColor]];
    [self.orView addSubview:orL];
        
    [self.view addSubview:self.orView];
    
    self.note =[[UILabel alloc]initWithFrame:CGRectMake(20, 220, 240, 30)];
    self.note.text=@"Already have an Account ? Log In";
    [self.note setFont:[UIFont systemFontOfSize:15]];
    self.note.textColor=[UIColor brownColor];
    
    [self.contenView addSubview:self.name];
    [self.contenView addSubview:self.mail];
    [self.contenView addSubview:self.pass];
    [self.contenView addSubview:self.nameField];
    [self.contenView addSubview:self.mailField];
    [self.contenView addSubview:self.passField];
    
    [self.contenView addSubview:self.note];
    [self.contenView addSubview:self.signUp];
    
    [self.view addSubview:self.contenView];
    
    self.logoImg =[[UIImageView alloc]initWithFrame:CGRectMake(60, 0, 200, 120)];
    self.logoImg.image =[UIImage imageNamed:@"logo"];
    [self.view addSubview:self.logoImg];
}

-(void)styleContent
{
    [StyleControl background:self.view];
    
    [self.contenView setBackgroundColor:[UIColor whiteColor]];
    [StyleControl setBorderStyle:(UIImageView*)self.contenView];
    [StyleControl setBorderStyle:(UIImageView *)self.signUp];
    [StyleControl drawLine:self.contenView PositionX:0 PositionY:50 Width:300];
    [StyleControl drawLine:self.contenView PositionX:0 PositionY:100 Width:300];
    [StyleControl drawLine:self.contenView PositionX:0 PositionY:150 Width:300];
    
    
    self.signUp.layer.borderWidth =0;
}

-(void)signAction
{
    User *user = [User instance];
    NSString *status = [user loginWS:self.mailField.text passwd:self.passField.text Name:self.nameField.text];
    if([status intValue] == 0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login"
                                                        message:@"Your account is wrong, please try again"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }else {
        EventListController *detail = [[EventListController alloc] init];
        [self presentViewController:detail animated:YES completion:NULL];
        [detail release];
    }
}
-(void)creatFaceSignUp
{

   
    
    FBLoginView *loginview = [[FBLoginView alloc] init];
    loginview.frame = CGRectMake(20, 110, 280, 40);
    loginview.delegate = self;
    
    for (id obj in loginview.subviews)
    {
        if ([obj isKindOfClass:[UIButton class]])
        {
            NSLog(@"go");
            UIButton * loginButton =  obj;
            [loginButton setFrame:CGRectMake(0, 0, 280, 40)];

            UIImage *loginImage = [UIImage imageNamed:@"signWithFace"];
            [loginButton setImage:loginImage forState:UIControlStateNormal];
            [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];

        }
        if ([obj isKindOfClass:[UILabel class]])
        {
            UILabel * loginLabel =  obj;
            [loginview setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
            loginLabel.textAlignment = UITextAlignmentCenter;
            [loginLabel setFrame:CGRectMake(0, 0, 0, 0)];

        }
    }
    [self.view addSubview:loginview];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGRect contentFrame;
    contentFrame = self.view.frame;
    contentFrame.origin.y=-130;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.view.frame=contentFrame;
    [UIView commitAnimations];
    
    
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    CGRect contentFrame;
    contentFrame = self.view.frame;
    contentFrame.origin.y=20;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.view.frame=contentFrame;
    [UIView commitAnimations];
    [textField resignFirstResponder];
    return YES;
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
    AppDelegate *appdelegate =[[UIApplication sharedApplication]delegate];
    appdelegate.isFace=1;
    [appdelegate.imageDataArray removeAllObjects];
    [appdelegate.imageUrlArray removeAllObjects];
    UserClass *unit =[[UserClass alloc]init];
    unit.userName=[user.first_name stringByAppendingString:[NSString stringWithFormat:@" %@",user.last_name]];
    unit.userId=user.id;
    unit.userEmail=[user.id stringByAppendingString:@"@facebook.com"];
    
    User *userModel = [User instance];
    [userModel loginFacebook:[NSString stringWithFormat:@"%@",user.id] Email:unit.userEmail Name:unit.userName];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:unit.userName forKey:@"userName"];
    [defaults setObject:unit.userId forKey:@"userId"];
    [defaults setObject:unit.userEmail forKey:@"userEmail"];

    
    FBRequest* friendsRequest = [FBRequest requestForMyFriends];
    [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                  NSDictionary* result,
                                                  NSError *error) {
        NSArray* friends = [result objectForKey:@"data"];
        [defaults setObject:friends forKey:@"friendList"];
         
    }];
    

    EventListController *detail = [[EventListController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tempArray =[[NSMutableArray alloc]init];
    self.friendInfo =[[NSMutableArray alloc]init];
    [self createContent];
    [self styleContent];
    [self creatFaceSignUp];
    
    self.mailField.text = @"";
    self.nameField.text = @"";
    self.passField.text = @"";
}
-(void)viewDidDisappear:(BOOL)animated
{
   
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
